//
//  ViewController.m
//  04-瀑布流
//
//  Created by J on 15/11/12.
//  Copyright © 2015年 JZJ. All rights reserved.
//

#import "ViewController.h"
#import "WaterflowLayout.h"
#import "ShopCell.h"
#import "MJExtension.h"
#import "Shop.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,weak)UICollectionView * collectionView;
@property(nonatomic,strong) NSMutableArray * shops;
@end

@implementation ViewController

- (NSMutableArray *)shops{
    
    if (_shops==nil) {
        
        self.shops = [NSMutableArray array];
        
    }
    
    return _shops;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    
    //初始化数据
    NSArray * shopArray= [Shop objectArrayWithFilename:@"1.plist"];
    
    [self.shops addObjectsFromArray:shopArray];
    
    //创建collectionView

    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:[[WaterflowLayout alloc]init]];//自定义布局

    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellWithReuseIdentifier:@"shop"];
    
    [self.view addSubview:collectionView];
    
    self.collectionView = collectionView;

}


#pragma mark --UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.shops.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ShopCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"shop" forIndexPath:indexPath];
    
    cell.shop= self.shops[indexPath.item];
    
    
    return cell;
    
    
}
@end
